package com.yy.tech.data.context;

import com.yy.tech.data.coder.decode.Decoder;
import com.yy.tech.data.coder.encode.Encoder;
import com.yy.tech.data.logger.context.LoggerContent;
import com.yy.tech.data.provider.DesensitizeProvider;
import com.yy.tech.data.provider.model.FindCondition;
import com.yy.tech.data.provider.model.SecretRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

import static com.yy.tech.data.context.SecretContent.*;

@Component
public class DesensitizeContext {

    @Autowired
    private ApplicationContext context;
    @Autowired
    private List<DesensitizeProvider> providers;

    @Autowired
    private Environment env;

    public SecretRule findRule(FindCondition condition){
        if (!CollectionUtils.isEmpty(providers)){
            for (DesensitizeProvider provider:providers){
                SecretRule rule = provider.findRule(condition);
                if (null != rule){
                    return  rule;
                }
            }
        }
        return null;
    }

    public boolean exclude(FindCondition condition){
        condition.setType(FindCondition.FindType.exclude);
        SecretRule rule = findRule(condition);
        return null != rule;
    }

    public Decoder findDecoderByDecoder(FindCondition condition){
        SecretRule rule = findRule(condition);
        if (null != rule){
            return getHttpDecoder(rule.getDecoder());
        }
        return null;
    }

    public Encoder findEncoderByRule(FindCondition condition){
        SecretRule rule = findRule(condition);
        if (null != rule){
            return getHttpEncoder(rule.getEncoder());
        }
        return null;
    }
    public Decoder getHttpDecoder(Class<? extends Decoder> clz,String name ){
        Decoder decoder = getDecoder(clz);
        if (null == decoder){
            decoder= getHttpDecoder(name);
        }
        return decoder;
    }

    public Encoder getHttpEncoder(Class<? extends Encoder> clz,String name ){
        Encoder encoder = getEncoder(clz);
        if (null == encoder){
            encoder= getHttpEncoder(name);
        }
        return encoder;
    }

    public Encoder getEncoder(Class<? extends Encoder> clz){
        if(null != clz && clz != Encoder.class){
            return context.getBean(clz);
        }
        return null;
    }

    public Decoder getDecoder(Class<? extends Decoder> clz){
        if(null != clz && clz != Decoder.class){
            return context.getBean(clz);
        }
        return null;
    }

    public Encoder getHttpEncoder(String name){
        Encoder encoder = getConverter(name,Encoder.class);
        if (null == encoder){
            encoder = getHttpEncoder();
        }
        return encoder;
    }
    public Encoder getHttpEncoder(){
        String name = env.getProperty(KEY_DESENSITIZE_SECRET_ENCODER);
        Encoder encoder = getConverter(name,Encoder.class);
        if (null == encoder){
            encoder = getDefaultEncoder();
        }
        return encoder;
    }

    public Decoder getHttpDecoder(String name){
        Decoder decoder = getConverter(name,Decoder.class);
        if (null == decoder){
            decoder = getHttpDecoder();
        }
        return decoder;
    }
    public Decoder getHttpDecoder(){
        String name = env.getProperty(KEY_DESENSITIZE_SECRET_DECODER);
        Decoder decoder = getConverter(name,Decoder.class);
        if (null == decoder){
            decoder = getDefaultDecoder();
        }
        return decoder;
    }

    public Decoder getParamDecoder(String name){
        Decoder decoder = getConverter(name,Decoder.class);
        if (null == decoder){
            decoder = getParamDecoder();
        }
        return decoder;
    }
    public Decoder getParamDecoder(){
        String name = env.getProperty(KEY_DESENSITIZE_PARAM_DECODER);
        Decoder decoder = getConverter(name,Decoder.class);
        if (null == decoder){
            decoder = getDefaultDecoder();
        }
        return decoder;
    }

    public Encoder getLoggerEncoder(String name){
        Encoder encoder = getConverter(name,Encoder.class);
        if (null == encoder){
            encoder = getLoggerEncoder();
        }
        return encoder;
    }
    public Encoder getLoggerEncoder(){
        String name = env.getProperty(LoggerContent.KEY_DESENSITIZE_LOGGER_ENCODER);
        Encoder encoder = getConverter(name,Encoder.class);
        if (null == encoder){
            encoder = getDefaultEncoder();
        }
        return encoder;
    }

    public Decoder getDefaultDecoder(){
        String name = env.getProperty(KEY_DESENSITIZE_DECODER);
        return getConverter(name, Decoder.class);
    }

    public Encoder getDefaultEncoder(){
        String name = env.getProperty(KEY_DESENSITIZE_ENCODER);
        return getConverter(name,Encoder.class);
    }

    public <T>T getConverter(final String name,Class<T> clz) {
        if (!StringUtils.isEmpty(name)) {
            return context.getBean(name, clz);
        }
        return null;
    }

    public ApplicationContext getContext() {
        return context;
    }
}
